//
//  YSRefreshView.swift
//  新浪浪
//
//  Created by mac on 16/3/7.
//  Copyright © 2016年 mac. All rights reserved.
//

import UIKit
import SnapKit

enum RefreshType: Int {
    case Normal = 0
    case Pulling = 1
    case Loding = 2
}

class YSRefreshView: UIControl {

    var scrollView: UIScrollView?
    var oldStatus: RefreshType = .Normal
    var status: RefreshType = .Normal{
        didSet{
            switch status {
            case .Normal: print("正常状态")
                messageLabel.text = "正常状态"
            case.Pulling: print("下拉释放")
                messageLabel.text = "下拉释放"
            case.Loding: print("加载中...")
                messageLabel.text = "加载中..."
                
                var insert = scrollView?.contentInset
                insert?.top += 64
                scrollView?.contentInset = insert!
                
                sendActionsForControlEvents(UIControlEvents.ValueChanged)
                oldStatus = .Loding
            }
        }
    }
    
    func endRefreshing(){
        status = .Normal
        if oldStatus == .Loding{
            var insert = scrollView?.contentInset
            insert?.top -= 64
            scrollView?.contentInset = insert!
            oldStatus = .Normal
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    private func setupUI(){
        var frame = self.frame
        frame.size = CGSizeMake(UIScreen.mainScreen().bounds.width, 50)
        frame.origin.y = -50
        self.frame = frame
        
        addSubview(messageLabel)
        addSubview(lodingImageView)
        
        
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_centerY)
        }
        lodingImageView.snp_makeConstraints { (make) -> Void in
            make.centerY.equalTo(self.snp_centerY)
            make.right.equalTo(messageLabel.snp_left)
        }
        
    }
    
    override func willMoveToSuperview(newSuperview: UIView?) {
        scrollView = newSuperview as? UIScrollView
        scrollView?.addObserver(self, forKeyPath: "contentOffset", options: NSKeyValueObservingOptions.New, context: nil)
    }
    override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
        
        let scrollVeiw = object as? UIScrollView
        if scrollView!.dragging {
            if scrollView?.contentOffset.y < -150 {
                status = .Pulling
                
            }else if scrollView?.contentOffset.y < -144 && scrollView?.contentOffset.y > -150{
                status = .Normal
            }
        } else {
            if scrollView?.contentOffset.y < -150 && status == .Pulling{
                status = .Loding
            }
        }
        
    }
    
    
    
    private lazy var messageLabel:UILabel = {
        let lab = UILabel()
        lab.text = "下拉刷新"
        lab.textColor = UIColor.redColor()
        lab.font = UIFont.systemFontOfSize(15)
        return lab
        }()
    private lazy var lodingImageView: UIImageView = {
        let ima = UIImageView()
        ima.image = UIImage (named: "tableview_loading")
        ima.hidden = true
        return ima
    }()

}







